package com.mccken.designpattern.state.sub;

import com.mccken.designpattern.state.GumballMachine;
import com.mccken.designpattern.state.State;

import cn.hutool.core.util.RandomUtil;

public class HasQuarterState implements State {
	GumballMachine gumballMachine;
	
	public HasQuarterState(GumballMachine gumballMachine) {
		this.gumballMachine = gumballMachine;
	}
	
	@Override
	public void insertQuarter() {
		System.out.println("HasQuarterState insertQuarter");
	}

	@Override
	public void ejectQuarter() {
		System.out.println("HasQuarterState ejectQuarter");
		gumballMachine.setState(gumballMachine.getNoQuarterState());
	}

	@Override
	public void turnCrank() {
		System.out.println("HasQuarterState turnCrank");
		int winner = RandomUtil.randomInt(0,10);
		if (winner == 0 && gumballMachine.getCount() > 1) {
			gumballMachine.setState(gumballMachine.getWinnerState());
		} else {
			gumballMachine.setState(gumballMachine.getSoldState());
		}
	}

	@Override
	public void dispense() {
		System.out.println("HasQuarterState dispense");
	}
}